<?php

namespace App\Console\Commands;
use App\CrmConfig;

use App\Customer;
use App\CustomerRemind;
use App\Notifications\CustomerRemindNotifications;
use App\Notifications\CustomerSeaNotifucations;
use App\ProjectUser;
use App\User;
use App\UserProject;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;

class NewSeaCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'newSea';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '新进入公海';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {     $remindConfig=CrmConfig::whereName('remind_config')->first();
        if ($remindConfig->value==1){
        Customer::chunkById(100,function ($customers){
            foreach ($customers as $customer){
                $customer=Customer::find($customer->customer_id);
                $this->info($customer->customer_id);
                //判断是否需要提醒
                if ($this->isRemindPool($customer)&&!CustomerRemind::whereOwnerUserId($customer->owner_user_id)
                        ->where('customer_id',$customer->customer_id)->first()){
                    $this->info('到期提醒'.$customer->customer_id);
                    $user=\App\User::find($customer->owner_user_id);
                    if ($user){
                        $user->notify(new CustomerRemindNotifications($customer));
                    }
                    $remind=new CustomerRemind();
                    $remind->customer_id=$customer->customer_id;
                    $remind->owner_user_id=$customer->owner_user_id;
                    $remind->save();
                }
                //判断公海
                $isPool=$this->isPool($customer);
                if ($isPool){//是公海
                    if ($customer->is_sea==0){
                        $customer->is_sea=1;
                        $customer->sea_time=time();
                   /*     $userProject = ProjectUser::where('project_id',$customer->project_id)
                            ->where('user_id','!=',$customer->owner_user_id)
                            ->inRandomOrder()
                            ->first();
                        if ($userProject){
                            $customer->owner_user_id =$userProject->user_id;
                            $customer->update_time=time();
                            $customer->deal_time=time();
                            $customer->create_time=time();
                            $customer->follow='待跟进';
                            $customer->status='潜在客户';
                            $customer->ro_user_id='';
                            $customer->rw_user_id='';
                            $customer->save();
                            continue;
                        }*/
                   /*     if (!$customer->is_send_sea){
                            //发送通知
                            $users=User::where('status',1)->where('is_display',0)->get();
                            foreach ($users as $user){
                                /**
                                 * @var  User $user
                                 */
                              /*  if ($user->id!=$customer->owner_user_id){
                                    $user->notify(new CustomerSeaNotifucations($customer));
                                }
                            }
                            $customer->is_send_sea=1;*/
                     /*   }*/
                        $this->info($customer->customer_id.'是公海');
                        $customer->save();
                    }
                }else{
                    if ($customer->is_sea==1){
                        $customer->is_sea=0;
                        $customer->sea_time=0;
                        $customer->is_send_sea=0;
                        $customer->save();
                        $this->info($customer->customer_id.'不是公海');
                    }
                }
                $this->info(Carbon::now()->toDateTimeString());
            }
        });
    }
    }
    public function isPool(Customer $customer){
        if ($customer->is_lock==1){
            return false;
        }
        if ($customer->deal_status=='已成交'){
            return false;
        }
        if ($customer->owner_user_id==0&&$customer->is_lock==0&&$customer->deal_status!='已成交'){
            return true;
        }

        return false;
    }
    public function isRemindPool(Customer $customer){
        if ($customer->customer_id==0){
            return false;
        }
        if ($customer->is_lock==1){
            return false;
        }
        return false;
    }
}
